<?php
/**
 * Created by PhpStorm.
 * userValidate: gh
 * Date: 2018/5/17
 * Time: 9:59
 */

namespace app\wap\behavior;


use redis\Redis;
use think\Config;
use think\Db;
use think\Session;
use think\Request;
use app\wap\model\Member;

class Behavior
{
    
    public function run(&$params)
    {
        $filter = \config('FrontBehavior.filter');
        $filter = array_map("strtolower", $filter);
        $request = request();
        $res_url = $request->dispatch()['module'];
        $bhv = $res_url[0] . '/' . $res_url[1] .'/' . $res_url[2];
        if(in_array(strtolower($bhv) , $filter) == true){
            $token = $request->header('token');
            if ($token) {
                $redis = Redis::instance();
                $user_info = json_decode($redis->hGet('user_info' , $token) ,true);
                $data_user = json_decode($redis->hGet('data_user' , $token) ,true);
                if($data_user){
                    $filter_data = \config('FrontBehavior.data_user');
                    $filter_data = array_map("strtolower", $filter_data);
                    if(in_array(strtolower($bhv) , $filter_data) == false){
                        abort(json([
                            'msg' => '暂无权限',
                            'status' => 202
                        ]));
                    }
                    $status = Session::get('user_id');
                    if(empty($status)){
                        abort(json([
                            'msg' => '账号异常请重新登录',
                            'status' => 202
                        ]));
                    }
                    $request->data_user = $data_user;
                }
                if ($user_info) {
                    $token = $redis->get('front_user:' . $user_info['id']);
                    if(!$token){
                        abort(json([
                            'msg' => '账号过期请重新登录',
                            'status' => 202
                        ]));
                    }
                    $status = Session::get('user_id');
                    if(empty($status)){
                        abort(json([
                            'msg' => '账号异常请重新登录',
                            'status' => 202
                        ]));
                    }
                    $filter_data = \config('FrontBehavior.data_user');
                    $filter_data = array_map("strtolower", $filter_data);
                    if(in_array(strtolower($bhv) , $filter_data) == true){
                        abort(json([
                            'msg' => '暂无权限',
                            'status' => 202
                        ]));
                    }
                    if($user_info['modify'] == true){
                        $redis->hDel('user_info', $user_info['token']);
                        $User = new Member();
                        $user_info = $User->getUserInfo(['front_token'=>$token]);
                        $redis->hSet('user_info', $token, json_encode($user_info));
                        $redis->set('front_user:' . $user_info['id'], $token);
                    }
                    unset($user_info['password']);
                    unset($user_info['pay_password']);
                    $request->user_info = $user_info;
                }
                if(empty($data_user) && empty($user_info)){
                    abort(json([
                        'msg' => '账号异常请重新登录',
                        'status' => 202
                    ]));
                }
            } else {
                abort(json([
                    'msg' => '请登录',
                    'status' => 201
                ]));
            }
        }
    }

}